Imports Cdm.DataCore

Partial Class SpecialFields
    Inherits System.Web.UI.Page

    Public Function FieldTypeList() As System.Collections.Generic.List(Of Cdm.DataCore.Admin.AdminSpecialFieldType)
        Return Cdm.DataCore.Admin.AdminSpecialFieldType.ListAll()
    End Function

    Public Function DisplayFieldType(ByVal fieldType As Integer) As String
        Dim i As Integer
        For i = 0 To FieldTypeList().Count - 1
            If FieldTypeList(i).FieldTypeId = fieldType Then
                Return FieldTypeList(i).FieldTypeName
            End If
        Next
        Return ""
    End Function

    Protected Sub SaveButton_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles SaveButton.Click

        Dim fieldName As TextBox = CType(SpecialFieldsFormView.FindControl("FieldNameTextBox"), TextBox)
        Dim fieldType As DropDownList = CType(SpecialFieldsFormView.FindControl("FieldTypeDropDownList"), DropDownList)
        Dim displayOrder As TextBox = CType(SpecialFieldsFormView.FindControl("DisplayOrderTextBox"), TextBox)
        Dim isRequired As CheckBox = CType(SpecialFieldsFormView.FindControl("RequiredCheckBox"), CheckBox)
        Dim isActive As CheckBox = CType(SpecialFieldsFormView.FindControl("ActiveCheckBox"), CheckBox)
        Dim newField As Cdm.DataCore.Admin.AdminSpecialField = New Cdm.DataCore.Admin.AdminSpecialField()

        If fieldName.Text = "" Then
            formError("Field Name must contain a value.")
            Exit Sub
        End If

        If Not IsNumeric(displayOrder.Text) Then
            formError("Display Order must be a valid number")
            Exit Sub
        End If

        newField.TableId = CInt(Me.SelectFormDropDownList.SelectedValue)
        newField.IsActive = isActive.Checked
        newField.IsRequired = isRequired.Checked
        newField.FieldName = fieldName.Text
        newField.FieldTypeId = CInt(fieldType.SelectedValue)
        newField.DisplayOrder = CInt(displayOrder.Text)
        newField.Persist()
        Me.SpecialFieldObjectDataSource.Select()
        SpecialFieldsScrollingGridView.DataBind()
        fieldName.Text = ""
        fieldType.SelectedIndex = 0
        displayOrder.Text = ""
        isRequired.Checked = False
        isActive.Checked = False
        SetStatus("Setting Saved")
        SetGridVisible(True)
        Me.AddButton.Visible = True
        Me.SpecialFieldsFormView.Visible = False
        Me.SaveButton.Visible = False
        Me.CancelButton.Visible = False
    End Sub

    Protected Sub AddButton_Click(ByVal sender As Object, _
                               ByVal e As System.EventArgs) _
                               Handles AddButton.Click

        SetGridVisible(False)
        Me.AddButton.Visible = False
        'Me.SubTitleLabel.Visible = False

        If ErrorSummary.Visible = True Then
            ErrorSummary.Visible = False
        End If

        Me.SpecialFieldsFormView.Visible = True
        Me.SaveButton.Visible = True
        Me.CancelButton.Visible = True

    End Sub

    Private Sub SetGridVisible(ByVal isVisible As Boolean)
        Me.SpecialFieldsScrollingGridView.Visible = isVisible
        Me.ActiveOnlyCheckBox.Visible = isVisible
        Me.ScreenFormLabel.Visible = isVisible
        Me.SelectFormDropDownList.Visible = isVisible
        Me.SubmitFormSelectButton.Visible = isVisible
    End Sub


    Protected Sub CancelButton_Click(ByVal sender As Object, _
                               ByVal e As System.EventArgs) _
                               Handles CancelButton.Click
        Me.SpecialFieldsFormView.Visible = False
        Me.SaveButton.Visible = False
        Me.CancelButton.Visible = False

        SetGridVisible(True)
        Me.AddButton.Visible = True
        SetStatus("Add has been cancelled.")

    End Sub

    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.SpecialFieldsScrollingGridView.DivClientId, CInt(Me.SpecialFieldsScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.SpecialFieldsScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(28)
    End Sub

    Private Sub ClearStatus()
        SetStatus("")
    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub


    Protected Sub formError(ByVal strError As String)

        Me.ErrorSummary.SetSingleError(strError)
        ErrorSummary.Visible = True

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        ClearStatus()
        If Not Page.IsPostBack Then
            Me.SpecialFieldsScrollingGridView.Sort("FieldName", SortDirection.Ascending)
        End If

        Me.SelectFormDropDownList.Attributes.Add("onchange", "document.getElementById(""mainSection"").style.visibility=""hidden"";document.getElementById(""addSection"").style.visibility=""hidden"";")


        If Me.ErrorSummary.Visible = True Then
            Me.ErrorSummary.Visible = False
        End If

    End Sub

    Protected Sub SpecialFieldsScrollingGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles SpecialFieldsScrollingGridView.RowCancelingEdit
        If ErrorSummary.Visible = True Then
            ErrorSummary.Visible = False
            SetGridVisible(True)
        End If
        Me.AddButton.Visible = True
        SetStatus("Edit cancelled.")
    End Sub

    Protected Sub SpecialFieldsScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles SpecialFieldsScrollingGridView.RowDataBound
        If e.Row.DataItemIndex = 0 Then
            AccessibilityController.AddImageToSortableGridHeaders(Me.SpecialFieldsScrollingGridView)
        End If

        ' Get a reference to the delete link button.
        Dim DeleteButton As LinkButton = e.Row.FindControl("DeleteButton")

        ' Determine we're dealing with a data row.
        If e.Row.RowType = DataControlRowType.DataRow Then
            DeleteButton.Visible = ((Cdm.DataCore.Admin.SpecialFieldValue.SpecialFieldValueCount(CInt(Me.SpecialFieldsScrollingGridView.DataKeys(e.Row.RowIndex).Values(0))) = 0) And (e.Row.Cells(0).Text <> "&nbsp;"))
        End If

        ' When entering edit mode set focus on the field type drop down list.
        Dim editFieldType As DropDownList = CType(e.Row.FindControl("editFieldType"), DropDownList)
        If Not editFieldType Is Nothing Then
            SetFocus(editFieldType.ClientID)
        End If

        Dim chk As CheckBox = e.Row.FindControl("EditActiveCheckBox")
        If chk IsNot Nothing Then
            chk.InputAttributes.Add("title", "check if field is active")
        End If
        chk = e.Row.FindControl("EditRequiredCheckBox")
        If chk IsNot Nothing Then
            chk.InputAttributes.Add("title", "check if field is required")
        End If
    End Sub

    Protected Sub SpecialFieldsScrollingGridView_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles SpecialFieldsScrollingGridView.RowDeleted
        SetStatus("")
    End Sub

    Protected Sub SpecialFieldsScrollingGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles SpecialFieldsScrollingGridView.RowEditing
        Me.AddButton.Visible = False
    End Sub

    Protected Sub SpecialFieldsScrollingGridView_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles SpecialFieldsScrollingGridView.RowUpdated
        Me.AddButton.Visible = True
        SetStatus("Special field has been updated successfully.")
    End Sub

    Protected Sub SpecialFieldsScrollingGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles SpecialFieldsScrollingGridView.RowUpdating
        Dim strNV As String
        Dim sValue As String
        ' Index of Field Name
        Dim iIdx As Integer = 0
        Dim strOV As String
        If e.OldValues.Item(iIdx) IsNot Nothing Then
            strOV = e.OldValues.Item(iIdx).ToString.ToUpper
        Else
            strOV = ""
        End If

        If e.NewValues.Item(iIdx) IsNot Nothing Then
            strNV = e.NewValues.Item(iIdx).ToString.ToUpper
            If strOV = strNV Then
                If Me.ErrorSummary.Visible = True Then
                    Me.ErrorSummary.Visible = False
                End If
            Else
                For i As Integer = 0 To SpecialFieldsScrollingGridView.Rows.Count - 1
                    If strNV = SpecialFieldsScrollingGridView.Rows(i).Cells(1).Text.ToUpper Then
                        sValue = "Value already exists in the database. Please enter a different value."
                        Me.ErrorSummary.SetSingleError(sValue)
                        SetStatus(sValue)
                        e.Cancel = True
                        Me.ErrorSummary.Visible = True
                        Exit Sub
                    End If
                Next

                'Passed error checking now update database
                Me.ErrorSummary.Visible = False
            End If
        Else
            ' Display error message for fields with no field name.
            If Cdm.DataCore.Admin.SpecialFieldValue.SpecialFieldValueCount(CInt(e.Keys.Item(0))) > 0 Then
                sValue = "You must enter a value in the field before updating."
                Me.ErrorSummary.SetSingleError(sValue)
                SetStatus(sValue)
                Me.ErrorSummary.Visible = True
                e.Cancel = True
            End If
        End If
    End Sub
End Class

